package com.sky.yipinyoufujava.controller;


import com.sky.yipinyoufujava.pojo.User;
import com.sky.yipinyoufujava.pojo.from.ShopsFrom;
import com.sky.yipinyoufujava.pojo.goods.*;
import com.sky.yipinyoufujava.pojo.from.GoodsForm;
import com.sky.yipinyoufujava.result.Result;
import com.sky.yipinyoufujava.service.GoodsService;
import com.sky.yipinyoufujava.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private LoginService loginService;
    /**
     * 获取商品，以及查询
     */
    @PostMapping("/getGoods")
    private Result getGoods(@RequestBody GoodsForm goodsForm){
        log.info("查询商品，{}",goodsForm);
        List<Goods> goods = goodsService.getGoods(goodsForm);
        return Result.success(goods);
    }

    /**
     * 获取商品详情
     * @param goodsId
     * @return
     */
    @PostMapping("/getGoodsInfo")
    private Result getGoodsInfo(@RequestBody Object goodsId){
        log.info("获取商品详情,{}",goodsId);
        Goods goods = goodsService.getGoodsInfo(goodsId);
//        获取海报图
        goods.setGoodsPoster(goodsService.getGoodsPoster(goodsId));
        // 获取店铺信息
        Shops shops = goodsService.getGoodsShops(goodsId);
//        获取商品图
        goods.setGoodsImage(goodsService.getGoodsImage(goodsId));
        goods.setGoodsShops(shops);
        return Result.success(goods);
    };
    /**
     * 添加商品
     */

    @PostMapping("/insertGoods")
    private Result insertGoods(@RequestBody Goods goods){
        log.info("添加商品，{}",goods);
        try {
            goodsService.insertGoods(goods);
            return Result.success("添加成功！");
        } catch (Exception e) {
            log.error("添加商品失败，{}", goods, e);
            return Result.error("添加失败！");
        }
    };

    /**
     * 更新商品内容
     */
    @PostMapping("/updateGoods")
    private Result updateGoods(@RequestBody Goods goods)
    {
        log.info("更新商品内容，{}",goods);
        try {
            goodsService.updateGoods(goods);
            return Result.success("修改成功！");
        } catch (Exception e) {
            log.error("修改商品失败，{}", goods, e);
            return Result.error("修改失败！");
        }
    }

    /**
     * 删除商品
     */

    @PostMapping("/deleteGoods")
    private Result deleteGoods(@RequestBody List<Short> ids){
        log.info("删除的商品，{}",ids);
        try {
            goodsService.deleteGoods(ids);
            return Result.success("删除成功！");
        } catch (Exception e) {
            log.error("删除商品失败，{}", ids, e);
            return Result.error("删除失败！");
        }
    };


//    查询某个商品的评价
    @PostMapping("/getGoodsComment")
    private Result getGoodsComment(@RequestBody Object goodsId){
        // 获取评价的基本信息
        List<GoodsComment> goodsComments = goodsService.getGoodsComment(goodsId);
        log.info("获取商品评价，{}",goodsComments);
//        循环
        for (GoodsComment goodsComment : goodsComments) {
            // 获取评价的用户信息
            User user = loginService.queryUserId(goodsComment.getScoreUserNameID());
            goodsComment.setScoreUserInfo(user);
            // 获取评价的标签
            List<GoodsLabel> goodsLabels = goodsService.getGoodsLabel(goodsComment.getId());
            goodsComment.setLabel(goodsLabels);

            // 获取评价的图片
            List<GoodsScoreImage> goodsCommentImages = goodsService.getGoodsCommentImage(goodsComment.getId());
            goodsComment.setScoreImage(goodsCommentImages);
        }
        // 获取评价的用户信息
        return Result.success(goodsComments);
    };


}
